/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Action;

import com.webaccess.retailws.Bean.CategoryBean;
import com.webaccess.retailws.Delegate.CategoryDelegate;
import com.webaccess.retailws.Util.CategoryUtil;
import com.webaccess.retailws.Util.CommonUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author Owner
 */
public class CategoryDispatchAction extends DispatchAction {

    private String SUCCESS = "success";
    CategoryDelegate categDelegate = new CategoryDelegate();
    CommonUtil commUtil = new CommonUtil();
    CategoryUtil categUtil = new CategoryUtil();
    ActionMessages errors = null;
    CategoryBean categBean = null;

    public ActionForward showAddCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("statusList", commUtil.getStatus());
            request.setAttribute("parentCategList", categDelegate.getSubCategoryLHMap());
            saveToken(request);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "addcategoryshow";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showEditCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        categBean = new CategoryBean();
        categDelegate = new CategoryDelegate();
        
        try {
            commUtil.getTopRightListMap(request);

            request.setAttribute("statusList", commUtil.getStatus());
            Long categoryId  = Long.parseLong(request.getParameter("categoryId"));
            request.setAttribute("parentCategList", categDelegate.getSubCategoryLHMap());
            categBean = categDelegate.getCategory(categoryId);
            request.setAttribute("category", categBean);
            saveToken(request);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "editcategoryshow";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward getParentCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        categBean = new CategoryBean();
        commUtil.getTopRightListMap(request);

        categBean = categDelegate.getParentCategoryList();
        request.setAttribute("parentCategoryList", categBean);

        SUCCESS = "showcategory";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward getSubCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long categoryId = 0L;
        categBean = new CategoryBean();
        commUtil.getTopRightListMap(request);
        categoryId = Long.parseLong(request.getAttribute("categoryId").toString());
        categBean = categDelegate.getSubCategoryList(categoryId);
        request.setAttribute("subCategoryList", categBean);

        SUCCESS = "showcategory";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward showManageCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        categBean = new CategoryBean();
        try {
            commUtil.getTopRightListMap(request);

            CategoryBean[] categBeanList = categDelegate.getAllCategory();
            request.setAttribute("categoryList", categBeanList);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "managecategoryshow";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {        
        try {
            commUtil.getTopRightListMap(request);

            request.setAttribute("CategoryList", categDelegate.getMainCategoryList());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "showonlinestore";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward searchCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        categDelegate = new CategoryDelegate();
        categBean = new CategoryBean();
        Long parentCategoryId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            parentCategoryId = Long.parseLong(request.getParameter("parentCategoryId"));
            if (parentCategoryId == 0) {
                SUCCESS = "categorysuccess";
            } else {
                CategoryBean[] categBeanList = categDelegate.searchCategory(parentCategoryId);
                request.setAttribute("categoryList", categBeanList);
                SUCCESS = "managecategoryshow";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward deleteCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        categDelegate = new CategoryDelegate();
        Long categoryId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            categoryId = Long.parseLong(request.getParameter("categoryId"));

            boolean isCategoryDeleted = categDelegate.deleteCategory(categoryId);
            if (isCategoryDeleted == true) {
                SUCCESS = "categorysuccess";
            } else {
                SUCCESS = "deletecategoryfailure";
            }
        } catch (Exception ex) {
            SUCCESS = "deletecategoryfailure";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward updateCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commUtil.getTopRightListMap(request);
        categDelegate = new CategoryDelegate();
        categUtil = new CategoryUtil();
        errors = new ActionMessages();

        String categoryName = "", categoryTitle = "", categoryDesc = "", filePath = "", categoryImageName = "";
        Long parentCategoryId = 0L, categoryStatusId = 0L, categoryId = 0L;
        int mainCategory = 0;
        try {
            DynaActionForm categDynaForm = (DynaActionForm) form;
            if (isTokenValid(request)) {
                categoryId = Long.parseLong(request.getParameter("categoryId"));
                categoryName = String.valueOf(categDynaForm.getString("categoryName"));
                categoryTitle = String.valueOf(categDynaForm.getString("categoryTitle"));
                categoryDesc = String.valueOf(categDynaForm.get("categoryDesc"));
                parentCategoryId = Long.parseLong(request.getParameter("parentCategoryId"));
                categoryStatusId = Long.parseLong(request.getParameter("categoryStatusId"));
                FormFile categoryImage = (FormFile) categDynaForm.get("categoryImage");
                categoryImageName = String.valueOf(categDynaForm.getString("categoryImageName"));
                if ((Boolean) categDynaForm.get("mainCategory") == true) {
                    mainCategory = 1;
                } else {
                    parentCategoryId = Long.parseLong(request.getParameter("parentCategoryId"));
                }
                Pattern fileType = Pattern.compile("/.*\\.(gif)|(jpeg)|(jpg)|(png)$/");
                Matcher matchFileType = fileType.matcher(categoryImage.getContentType());

                if (categoryName.equalsIgnoreCase("") || categoryTitle.equalsIgnoreCase("") || categoryDesc.equalsIgnoreCase("") || (mainCategory == 0 && parentCategoryId == 0) || categoryStatusId == 0) {
                    errors.add("errCategory", new ActionMessage("errors.category.required"));
                } else if (categoryImage.getFileSize() != 0) {
                    if (!matchFileType.find()) {
                        errors.add("errCategoryImage", new ActionMessage("errors.categoryimage.filetype"));
                    } else if (categoryImage.getFileSize() > 1048576) { //1MB
                        errors.add("errCategoryImage", new ActionMessage("errors.categoryimage.size"));
                    } else {
                        filePath = getServlet().getServletContext().getRealPath("/") + "category";
                        categoryImageName = categUtil.uploadFile(categoryImage, filePath);

                        SUCCESS = categUtil.saveCategory(categoryId, categoryName, categoryTitle, categoryDesc, categoryStatusId, categoryImageName, parentCategoryId, mainCategory);
                    }
                } else {

                    SUCCESS = categUtil.saveCategory(categoryId, categoryName, categoryTitle, categoryDesc, categoryStatusId, categoryImageName, parentCategoryId, mainCategory);
                }
                resetToken(request);
                saveErrors(request, errors);
                if (errors.isEmpty()) {
                    SUCCESS = "categorysuccess";
                } else {
                    SUCCESS = "updatecategoryfailure";
                }
            }
            saveToken(request);
        } catch (Exception ex) {
            SUCCESS = "updatecategoryfailure";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }

    }

    public ActionForward saveCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commUtil.getTopRightListMap(request);
        categDelegate = new CategoryDelegate();
        categUtil = new CategoryUtil();
        errors = new ActionMessages();

        String categoryName = "", categoryTitle = "", categoryDesc = "", filePath = "", categoryImageName = "";
        Long parentCategoryId = 0L, categoryStatusId = 0L;
        int mainCategory = 0;
        try {
            DynaActionForm categDynaForm = (DynaActionForm) form;
            if (isTokenValid(request)) {
                categoryName = String.valueOf(categDynaForm.getString("categoryName"));
                categoryTitle = String.valueOf(categDynaForm.getString("categoryTitle"));
                categoryDesc = String.valueOf(categDynaForm.get("categoryDesc"));
                categoryStatusId = Long.parseLong(request.getParameter("categoryStatusId"));
                FormFile categoryImage = (FormFile) categDynaForm.get("categoryImage");
                if ((Boolean) categDynaForm.get("mainCategory") == true) {
                    mainCategory = 1;
                } else {
                    parentCategoryId = Long.parseLong(request.getParameter("parentCategoryId"));
                }
                Pattern fileType = Pattern.compile("/.*\\.(gif)|(jpeg)|(jpg)|(png)$/");
                Matcher matchFileType = fileType.matcher(categoryImage.getContentType());

                if (categoryName.equalsIgnoreCase("") || categoryTitle.equalsIgnoreCase("") || categoryDesc.equalsIgnoreCase("") || (mainCategory == 0 && parentCategoryId == 0) || categoryStatusId == 0) {
                    errors.add("errCategory", new ActionMessage("errors.category.required"));
                } else if (categoryImage.getFileSize() != 0) {
                    if (!matchFileType.find()) {
                        errors.add("errCategoryImage", new ActionMessage("errors.categoryimage.filetype"));
                    } else if (categoryImage.getFileSize() > 1048576) { //1MB
                        errors.add("errCategoryImage", new ActionMessage("errors.categoryimage.size"));
                    } else {
                        filePath = getServlet().getServletContext().getRealPath("/") + "category";
                        categoryImageName = categUtil.uploadFile(categoryImage, filePath);

                        SUCCESS = categUtil.saveCategory(null, categoryName, categoryTitle, categoryDesc, categoryStatusId, categoryImageName, parentCategoryId, mainCategory);
                    }
                } else {
                    categoryImageName = "ImageNotFound.jpg";

                    SUCCESS = categUtil.saveCategory(null, categoryName, categoryTitle, categoryDesc, categoryStatusId, categoryImageName, parentCategoryId, mainCategory);
                }
                resetToken(request);
                saveErrors(request, errors);
                if (errors.isEmpty()) {
                    SUCCESS = "categorysuccess";
                } else {
                    SUCCESS = "savecategoryfailure";
                }
            }
            saveToken(request);
        } catch (Exception ex) {
            SUCCESS = "savecategoryfailure";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }

    }
}
